package java课题设计.功能类.Seek;
import java课题设计.工具类.Seek;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
//本类用于通过学生姓名来搜索学生
public class SeekName extends JFrame implements MouseListener {
 
    Font f=new Font("宋体",Font.BOLD,25);
    String name;
    private JFrame pre;
    private JTable table;
    JButton seek=new JButton("查找");
    public SeekName(JFrame pre){
        extracted();
        initImage();
        this.setVisible(true);
    }
    public void initImage(){
        //添加输入学号提示框
        JLabel Name=new JLabel("请输入你要查找的姓名");
        Name.setFont(f);
        Name.setBounds(75,40,400,100);
        this.getContentPane().add(Name);
 
 
        //输入学号输入文本框
        JTextField Nametext=new JTextField();
        Nametext.setBounds(75,140,250,50);
        this.getContentPane().add(Nametext);
        //添加监听器
        Nametext.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                name = Nametext.getText();
            }
 
            @Override
            public void removeUpdate(DocumentEvent e) {
                name = Nametext.getText();
            }
 
            @Override
            public void changedUpdate(DocumentEvent e) {
                name = Nametext.getText();
            }
        });
 
        //添加查找按钮
        seek.setFont(f);
        seek.setBounds(100,220,140,45);
        this.getContentPane().add(seek);
 
    }
    public void extracted(){
        //设置登录界面的长和宽
        this.setSize(500,450);
        this.setTitle("请输入你要查找的学生的姓名");
        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(3);
        this.setLayout(null);
        this.setLocationRelativeTo(null);
 
        //绑定监听器
        seek.addMouseListener(this);
    }
    public void chazhao(String stuname) throws IOException {
        boolean flag= Seek.seek4(stuname);
        Seek.seekSize(stuname);
 
        if(flag==false){
            List<String> results=new ArrayList<>();
            results=Seek.findByName(stuname);
            // 创建表格
            String[] columnNames = {"学生姓名","性别", "学号", "体育成绩", "Java成绩", "高数成绩", "出生年月日"};
            DefaultTableModel model = new DefaultTableModel(columnNames, 1000);
            table = new JTable(model);
            int i=0;
            for (String s : results) {
                //将取出的数据进行分割
                String[] fields = s.split("\t");
                String name = fields[0];
                String sex = fields[1];
                String sid = fields[2];
                String PEscore = fields[3];
                String Javascore = fields[4];
                String Mathscore = fields[5];
                String birthDate = fields[6];
 
                //将学生数据添加到表格中
                Object[] rowData = {name,sex, sid, PEscore, Javascore, Mathscore, birthDate};
                model.setValueAt(name, i, 0);
                model.setValueAt(sex,i,1);
                model.setValueAt(sid, i, 2);
                model.setValueAt(PEscore, i, 3);
                model.setValueAt(Javascore, i, 4);
                model.setValueAt(Mathscore, i, 5);
                model.setValueAt(birthDate, i, 6);
                i++;
            }
            // 创建 JScrollPane
            JScrollPane scrollPane = new JScrollPane(table);
 
            // 创建 JFrame 并展示 UI 界面
            JFrame frame = new JFrame("学生信息表");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);
 
            JPanel contentPane = new JPanel(new BorderLayout());
            contentPane.add(scrollPane, BorderLayout.CENTER);
            frame.setContentPane(contentPane);
 
            frame.pack();
            frame.setVisible(true);
        }
        else{
            //跳出弹窗查找失败
            System.out.println("查找失败");
            JDialog wrong=new JDialog(this,null,true);
            wrong.setBounds(550,300,300,200);
            wrong.setTitle("未找到该学生");
            JLabel w=new JLabel("未找到该学生");
            w.setFont(new Font("宋体",Font.BOLD,18));
            w.setBounds(100,100,100,100);
            wrong.add(w);
            wrong.setAlwaysOnTop(true);
            wrong.setVisible(true);
 
        }
    }
 
    @Override
    public void mouseClicked(MouseEvent e) {
        if(e.getSource()==seek){
            System.out.println("点击了查找按钮");
            //调用查找方法
            try {
                chazhao(name);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
 
    @Override
    public void mousePressed(MouseEvent e) {
 
    }
 
    @Override
    public void mouseReleased(MouseEvent e) {
 
    }
 
    @Override
    public void mouseEntered(MouseEvent e) {
 
    }
 
    @Override
    public void mouseExited(MouseEvent e) {
 
    }
}